/*
 * Timer.h
 *
 *  Created on: Oct 9, 2014
 *      Authors: Avihay Avitan & Stefan Farcas
 */
#include <iostream>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <boost/thread.hpp>
#include "../GSLogger/Logger.h"
#include "../PassLogger/PassLogger.h"
#include "TimeoutHandler.h"

using namespace std;
using namespace LoggerNameSpace;

#ifndef TIMER_H_
#define TIMER_H_

namespace TimerNameSpace {



typedef void (*TimerHandler)(sigval_t signum);

/**
 * TimerTimeoutHandler defines the interface for handling the timeout events in the pass process
 */
class TimerTimeoutHandler
{
	public:
		TimerTimeoutHandler() { };
		virtual ~TimerTimeoutHandler() { };
		virtual void handlerFunction( void ) = 0;
};


/**
 * Timer- a class that implements the timer mechanism
 */
class Timer
{
    public:
        Timer( TimerTimeoutHandler * timeHandler );
        ~Timer();

        void setDuration(long int seconds);
        void start();
        void restart();
        void timeout();
        void stop();
        void callbackWrapper( void );
        static void timeOutHandler( sigval_t This  );

    private:
        void createTimer(timer_t *timerid, TimerHandler handler_cb);
        void startTimer(timer_t timerid, int startTimeout, int cyclicTimeout);
        void stopTimer(timer_t timerid);

    long int m_Duration;
        TimerTimeoutHandler * timeOutHandlerImp;
        timer_t timerid;
};

} /* namespace TimerNameSpace */

#endif /* TIMER_H_ */
